#include "ext_rational.h"

inline int gcd(int a, int b){
  return (b == 0) ? a : gcd(b, a%b);
}

ext_rational::ext_rational() : num(0), denum(1), p(0){}

ext_rational::ext_rational(int x) : num(x), denum(1), p(1){}

ext_rational::ext_rational(int nn, int dd, int pp) : num(nn), denum(dd), p(pp) {}

const ext_rational ext_rational::operator* (const ext_rational& other) const{
  int nn = num * other.num;
  int dd = denum * other.denum;
  int pp = p & other.p;
  int g = gcd(nn, dd);
  return ext_rational(nn/g, dd/g, pp);
}

const bool ext_rational::operator <(const ext_rational& other){
  return 2 * (num * other.denum) + p < 2 * (denum * other.num) + other.p;
}

const bool ext_rational::operator <=(const ext_rational& other){
  return 2 * (num * other.denum) + p <= 2 * (denum * other.num) + other.p;
} 

const bool ext_rational::operator ==(const ext_rational &other){
  return 2 * (num * other.denum) + p == 2 * (denum * other.num) + other.p;
}

const int ext_rational::intval() const{
  return num / denum;
}